package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author wanghaotian
 * time:2022/12/16,0016 9:02
 * description:
 */

@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    private DtsRoleMapper roleMapper;

    @Autowired
    private DtsAdminMapper adminMapper;

    @Autowired
    private DtsPermissionMapper permissionMapper;

    @Override
    public Set<String> findRoleByRolesId(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DtsRole::getId,roleIds);
        wrapper.eq(DtsRole::getDeleted,false);
        List<DtsRole> dtsRoles = roleMapper.selectList(wrapper);
        Set<String> roles = dtsRoles.stream().map((role) -> {
            return role.getName();
        }).collect(Collectors.toSet());
        return roles;
    }

    @Override
    public List<DtsRole> findAll() {
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsRole::getDeleted,false);
        return roleMapper.selectList(wrapper);
    }

    @Override
    public IPage<DtsRole> findPage(Integer page, Integer limit, String name, String sort, String order) {
        IPage<DtsRole> iPage = new Page<>(page,limit);
        QueryWrapper<DtsRole> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        wrapper.eq("deleted", false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("asc")){
                wrapper.orderByAsc(sort);
            }else {
                wrapper.orderByDesc(sort);
            }
        }
        iPage = roleMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public void add(DtsRole role) {
        String name = role.getName();
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsRole::getName,name);
        wrapper.eq(DtsRole::getDeleted,false);
        DtsRole dtsRole = roleMapper.selectOne(wrapper);
        if (dtsRole != null) {
            throw new RuntimeException("角色已存在");
        }
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.insert(role);
    }

    @Override
    public void delete(DtsRole role) {
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(DtsAdmin::getRoleIds,role.getId());
        wrapper.eq(DtsAdmin::getDeleted,false);
        List<DtsAdmin> adminList = adminMapper.selectList(wrapper);
        if(adminList.size() > 0){
            throw new RuntimeException("角色绑定管理员不能删除");
        }
        roleMapper.deleteById(role);
    }

    @Override
    public void update(DtsRole role) {
        role.setUpdateTime(new Date());
        roleMapper.updateById(role);
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole dtsRole = roleMapper.selectById(roleId);
        return dtsRole.getName().equals("超级管理员");
    }

    @Override
    public Set<String> selectPermissions(Integer roleId) {
        LambdaQueryWrapper<DtsPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsPermission::getRoleId,roleId);
        wrapper.eq(DtsPermission::getDeleted,false);
        List<DtsPermission> permissionList = permissionMapper.selectList(wrapper);
        Set<String> perms = permissionList.stream().map((perm) -> {
            return perm.getPermission();
        }).collect(Collectors.toSet());
        return perms;
    }
}
